package business_opportunity

import (
	"git.mycaigou.com/gfyx/micro-gfyx-api/http_server/controller"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/errcode"
	dto "git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/business_opportunity"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/utils"
	"git.mycaigou.com/gfyx/micro-gfyx-api/service/business_opportunity/market_insight"
	"github.com/gin-gonic/gin"
)

type MarketInsightController struct {
	controller.BaseController
	marketInsightService market_insight.MarketInsightServiceIface
}

// NewMarketInsightController 构造函数
func NewMarketInsightController(
	marketInsightService market_insight.MarketInsightServiceIface,
) *MarketInsightController {
	return &MarketInsightController{
		marketInsightService: marketInsightService,
	}
}

// RegisterRouter
// 注册路由
func (c *MarketInsightController) RegisterRouter(g *gin.RouterGroup) {
	// 拿地、在建、开盘的柱状图接口
	g.GET("market-insight/project-histogram", c.projectHistogram)
	// 拿地、在建、开盘的top企业列表接口
	g.GET("market-insight/project-top20-company", c.projectTop20Company)
	// 折线图-土地交易、在建项目、开盘项目、招中标的折线图
	g.GET("market-insight/line-chart", c.lineChart)
	// 招中标top企业列表
	g.GET("market-insight/bid-top20-company", c.bidTop20Company)
	// 招中标柱状图
	g.GET("market-insight/bid-histogram", c.bidHistogram)
	// 客户结构饼图
	g.GET("market-insight/customer-pie-chart", c.customerPieChart)
	// 好客户top列表
	g.GET("market-insight/customer-top10", c.customerTop10)
	g.GET("market-insight/clear-cache", c.clearCache)
}

func (c *MarketInsightController) projectHistogram(ctx *gin.Context) {
	var request dto.GetProjectHistogramRequest
	if err := ctx.ShouldBind(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}

	resp, err := c.marketInsightService.GetProjectHistogram(ctx, &request)
	if err != nil {
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}

	c.Success(ctx, resp)
}

func (c *MarketInsightController) projectTop20Company(ctx *gin.Context) {
	var request dto.GetProjectHistogramRequest
	if err := ctx.ShouldBind(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}

	resp, err := c.marketInsightService.GetProjectTop20Company(ctx, &request)
	if err != nil {
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}

	c.Success(ctx, resp)
}

func (c *MarketInsightController) lineChart(ctx *gin.Context) {
	var request dto.MarketInsightCommonRequest
	if err := ctx.ShouldBind(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}

	resp, err := c.marketInsightService.LineChart(ctx, &request)
	if err != nil {
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}

	c.Success(ctx, resp)
}

func (c *MarketInsightController) bidTop20Company(ctx *gin.Context) {
	var request dto.MarketInsightCommonRequest
	if err := ctx.ShouldBind(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}

	resp, err := c.marketInsightService.GetBidTop20Company(ctx, &request)
	if err != nil {
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}

	c.Success(ctx, resp)
}

func (c *MarketInsightController) bidHistogram(ctx *gin.Context) {
	var request dto.MarketInsightCommonRequest
	if err := ctx.ShouldBind(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}

	resp, err := c.marketInsightService.BidHistogram(ctx, &request)
	if err != nil {
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}

	c.Success(ctx, resp)
}

func (c *MarketInsightController) customerPieChart(ctx *gin.Context) {
	var request dto.MarketInsightCommonRequest
	if err := ctx.ShouldBind(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}

	resp, err := c.marketInsightService.CustomerPieChart(ctx, &request)
	if err != nil {
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}

	c.Success(ctx, resp)
}

func (c *MarketInsightController) customerTop10(ctx *gin.Context) {
	var request dto.MarketInsightCommonRequest
	if err := ctx.ShouldBind(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}

	resp, err := c.marketInsightService.CustomerTop10(ctx, &request)
	if err != nil {
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}

	c.Success(ctx, resp)
}

func (c *MarketInsightController) empty(ctx *gin.Context) {
	c.Success(ctx, "todo")
}

func (c *MarketInsightController) clearCache(ctx *gin.Context) {
	err := c.marketInsightService.ClearMarketInsightCache(ctx)
	if err != nil {
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}
	c.Success(ctx, nil)
}
